package com.zsd.android.rtmpdemo;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.zsd.android.rtmpdemo.bean.CameraHelper;
import com.zsd.android.rtmpdemo.utils.PermissionHelper;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private SurfaceView mSurfaceView;
    private Button changeBtn;

    private CameraHelper cameraHelper;
    private long changeTimeOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSurfaceView = findViewById(R.id.main_surface);
        changeBtn = findViewById(R.id.main_change_btn);
        String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        PermissionHelper.getInstance(this).isPermission(this, permissions, 200);
        cameraHelper = new CameraHelper(this);
        cameraHelper.setSurfaceHolder(mSurfaceView.getHolder());
        changeBtn.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraHelper.closeCamera();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_change_btn:
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis - changeTimeOut > 2 * 1000) {
                    cameraHelper.changeCamera();
                    changeTimeOut = currentTimeMillis;
                }
                break;
        }
    }
}
